package com.mvd.app.ac2019;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kongzue.dialog.v3.TipDialog;
import com.mvd.app.R;
import com.mvd.app.ac2020.DiaoDuActivity;
import com.mvd.app.ac2025.HuanBaoVideoActivity;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.activity.DaiJianCarsActivity;
import com.mvd.app.activity.SettingActivity;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.UserAccountModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新版进入后的第一个页面
 * 导航页
 * 四个大按钮
 **/
public class FirstActivity extends BaseActivity {

    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.ac_first_waijian)
    TextView acFirstWaijian;
    @BindView(R.id.ac_login_view_m)
    TextView acLoginViewM;
    @BindView(R.id.ac_first_dipandongtai)
    TextView acFirstDipandongtai;
    @BindView(R.id.ac_first_lushi)
    TextView acFirstLushi;
    @BindView(R.id.ac_login_view_m123)
    TextView acLoginViewM123;
    @BindView(R.id.ac_first_setting)
    TextView acFirstSetting;
    @BindView(R.id.ac_first_digou_item)
    TextView acFirstDigouItem;
    @BindView(R.id.ac_first_digou)
    TextView acFirstDigou;
    @BindView(R.id.ac_first_huanbao_video)
    TextView acFirstHv;

    private FirstActivity instance;   private String Login_name = "", login_IDNumber, login_username;//当前登录的检测员的名字
    private UserAccountModel UserAccountModel_model;
    private int CHECK_MODE_CURRENT = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        ButterKnife.bind(this);
        instance = this;

        Login_name = getIntent().getExtras().getString("login_UserID", "");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        login_username = getIntent().getExtras().getString("login_username", "");

        UserAccountModel_model = (UserAccountModel) getIntent().getSerializableExtra("UserAccountModel_model");
        UtilsLog.e("FirstActivity-----UserAccountModel_model===" + UserAccountModel_model.toString());
        String logStr = "\n" + "---FirstActivity----onCreate---" +
                "\n" + "UserAccountModel_model::" + UserAccountModel_model.toString();
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
    }

    //  用户权限过滤
    boolean userRightCheckMode(int CHECK_MODE) {
        if (UserAccountModel_model == null)
            return false;

        boolean Is_exist = false;
        String userRight = UserAccountModel_model.getUserRight();
        UtilsLog.e("userRight==" + userRight);
        switch (CHECK_MODE) {
            case 3:
                //外检员
                if (userExistright(userRight, "0") || userExistright(userRight, "3")
                        || userExistright(userRight, "d") || userExistright(userRight, "D")) {
                    Is_exist = true;
                }
                break;
            case 4:
                //底盘动态
                if (userExistright(userRight, "0") || userExistright(userRight, "4")) {
                    Is_exist = true;
                }
                break;
            case 6:
                //路试
                if (userExistright(userRight, "0") || userExistright(userRight, "6")) {
                    Is_exist = true;
                }
                break;
            case 5:
                //底盘静态
                if (userExistright(userRight, "0") || userExistright(userRight, "5")) {
                    Is_exist = true;
                }
                break;
            case 7:
                //调度员
                if (userExistright(userRight, "0") || userExistright(userRight, "7")) {
                    Is_exist = true;
                }
                break;
        }
        return Is_exist;
    }

    //判断是否有对应的权限，有:true,否:false
    boolean userExistright(String userright, String right) {
        boolean is_userExistright = false;
        if (userright.contains(right)) {
            is_userExistright = true;
        }
        return is_userExistright;
    }

    @OnClick({R.id.ac_first_waijian, R.id.ac_first_dipandongtai,
            R.id.ac_first_lushi, R.id.ac_first_setting,
            R.id.ac_first_digou_item, R.id.ac_first_digou
            , R.id.ac_first_huanbao_video})
    public void onViewClicked(View view) {
        Intent intent = null;
        String logStr = "\n" + "---FirstActivity----onViewClicked---" +
                "\n" + "userRightCheckMode::" ;
        switch (view.getId()) {
            case R.id.ac_first_waijian:
//                intent = new Intent(instance, LoginActivity.class);
                BaseApplication.JIANCE_MODE = 0;
                CHECK_MODE_CURRENT = 3;//外检员
                if(userRightCheckMode(CHECK_MODE_CURRENT)){
                    intent = new Intent(instance, DaiJianCarsActivity.class);
                    intent.putExtra("userRightCheckMode", true);
                }
                logStr+=userRightCheckMode(CHECK_MODE_CURRENT)+"\n";
                break;
            case R.id.ac_first_dipandongtai:
//                intent = new Intent(instance, DaiJianCarsActivity.class);
                BaseApplication.JIANCE_MODE = 2;
                CHECK_MODE_CURRENT = 4;//底盘动态
                if(userRightCheckMode(CHECK_MODE_CURRENT)){
                    intent = new Intent(instance, DaiJianCarsActivity.class);
                    intent.putExtra("userRightCheckMode", false);
                }
                logStr+=userRightCheckMode(CHECK_MODE_CURRENT)+"\n";
                break;
            case R.id.ac_first_lushi:
//                intent = new Intent(instance, DaiJianCarsActivity.class);
                BaseApplication.JIANCE_MODE = 1;
                CHECK_MODE_CURRENT = 6;//路试
                if(userRightCheckMode(CHECK_MODE_CURRENT)){
                    intent = new Intent(instance, DaiJianCarsActivity.class);
                    intent.putExtra("userRightCheckMode", false);
                }
                logStr+=userRightCheckMode(CHECK_MODE_CURRENT)+"\n";
                break;
            case R.id.ac_first_setting:
//                intent = new Intent(instance, SettingActivity.class);地沟
//                intent = new Intent(instance, DaiJianCarsActivity.class);
                BaseApplication.JIANCE_MODE = 6;
                CHECK_MODE_CURRENT = 5;//底盘静态
                if(userRightCheckMode(CHECK_MODE_CURRENT)){
                    intent = new Intent(instance, DaiJianCarsActivity.class);
                    intent.putExtra("userRightCheckMode", false);
                }
                logStr+=userRightCheckMode(CHECK_MODE_CURRENT)+"\n";
                break;
            case R.id.ac_first_digou_item:
                //调度
//                intent = new Intent(instance, DiaoDuActivity.class);
                BaseApplication.JIANCE_MODE = 0;
                CHECK_MODE_CURRENT = 7;//调度员
                if(userRightCheckMode(CHECK_MODE_CURRENT)){
                    intent = new Intent(instance, DiaoDuActivity.class);
                    intent.putExtra("userRightCheckMode", false);
                }
                logStr+=userRightCheckMode(CHECK_MODE_CURRENT)+"\n";
                break;
            case R.id.ac_first_digou:
                //设置
                intent = new Intent(instance, SettingActivity.class);
//                BaseApplication.JIANCE_MODE = 0;
                break;
            case R.id.ac_first_huanbao_video:
                //环保视频
                intent = new Intent(instance, HuanBaoVideoActivity.class);
//                BaseApplication.JIANCE_MODE = 0;
                break;
        }

        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        if (intent != null) {
            intent.putExtra("check_mode", BaseApplication.JIANCE_MODE);
            intent.putExtra("login_UserID", Login_name);

            UtilsLog.e("getDataToLogin-login_IDNumber==" + login_IDNumber);
            intent.putExtra("login_IDNumber", login_IDNumber);
            startActivity(intent);
        }else{
            TipDialog.show(instance,"没有权限，请联系管理员", TipDialog.TYPE.WARNING);
        }
    }

    private long exitTime = 0;

    // 双击返回退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                BaseApplication.getSelf().exit(this);
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
